package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.dataConfig.ResponseStatus;
import com.dongdongshop.dataConfig.dataResult;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.pojo.TbGoodsVo;
import com.dongdongshop.service.ContentCategoryService;
import com.dongdongshop.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("category")
public class CategoryController {
    private Logger logger = LoggerFactory.getLogger(CategoryController.class);
    @Reference
    private ContentCategoryService contentCategoryService;

    @RequestMapping("getinfo")
    @ResponseBody
    public dataResult getinfo(@RequestParam(defaultValue = "1")Integer number, @RequestParam(defaultValue = "6")Integer pageSize, String sname){
        try {
            PageBean<TbContentCategory> b = contentCategoryService.getAll(number,pageSize,sname);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),b);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("toAdd")
    @ResponseBody
    public dataResult toAdd(TbContentCategory tbContentCategory){
        try {
            Integer i  = contentCategoryService.addinfo(tbContentCategory);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public dataResult deleteAll(@RequestParam("ids[]")Long[] ids){
        try {
            Integer i  = contentCategoryService.deleteAll(ids);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public dataResult toUpdate(Long id){
        try {
            TbContentCategory tb = contentCategoryService.toUpdate(id);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),tb);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("updateinfo")
    @ResponseBody
    public dataResult updateinfo(TbContentCategory tbContentCategory){
        try {
            Integer i  = contentCategoryService.updateinfo(tbContentCategory);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }
}
